﻿using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using SqlSugar;
using SqlSugar.Extensions;

namespace ConsoleApp.industry
{
    public class IndustryInit
    {
        public static List<JsonModel> GetJsonModel()
        {
            const string wordTemplateName = "D:/jsonstr.json";
            var sr = File.OpenText(wordTemplateName);
            var jsonWordTemplate = sr.ReadToEnd();
            sr.Dispose();
            var list = JsonConvert.DeserializeObject<List<JsonModel>>(jsonWordTemplate);
            return list;
        }

        public static void Init()
        {
            var db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "server=121.196.23.49;Database=crm_yddsj_net;Uid=root;Pwd=bca179e83f23af41;SslMode=none;",
                DbType = DbType.MySql,
                IsAutoCloseConnection = true
            });

            try
            {
                var list = GetJsonModel();

                db.BeginTran();

                foreach (var item1 in list)
                {
                    var model1 = new IndustryModel();
                    var item1Id = Guid.NewGuid().ToString("N");
                    model1.id = item1Id;
                    model1.name = item1.label;
                    model1.level = 1;
                    model1.sort = item1.value.ObjToInt();
                    model1.parent_id = "-1";
                    var nodelist1 = JsonConvert.SerializeObject(model1);
                    model1.node_list = "[" + nodelist1 + "]";
                    db.Insertable(model1).ExecuteCommand();
                    if (item1.children != null)
                    {
                        foreach (var item2 in item1.children)
                        {
                            var model2 = new IndustryModel();
                            var item2Id = Guid.NewGuid().ToString("N");
                            model2.id = item2Id;
                            model2.name = item2.label;
                            model2.level = 2;
                            model2.sort = item2.value.ObjToInt();
                            model2.parent_id = item1Id;
                            var nodelist2 = nodelist1 + "," + JsonConvert.SerializeObject(model2);
                            model2.node_list = "[" + nodelist2 + "]";
                            db.Insertable(model2).ExecuteCommand();

                            if (item2.children != null)
                            {
                                foreach (var item3 in item2.children)
                                {
                                    var model3 = new IndustryModel();
                                    var item3Id = Guid.NewGuid().ToString("N");
                                    model3.id = item3Id;
                                    model3.name = item3.label;
                                    model3.level = 3;
                                    model3.sort = item3.value.ObjToInt();
                                    model3.parent_id = item2Id;
                                    var nodelist3 = nodelist2 + "," + JsonConvert.SerializeObject(model3);
                                    model3.node_list = "[" + nodelist3 + "]";
                                    db.Insertable(model3).ExecuteCommand();
                                }
                            }
                        }
                    }
                }

                db.CommitTran();
            }
            catch (Exception ex)
            {
                db.RollbackTran();
                Console.WriteLine(ex.Message);
            }
        }
    }
}
